package com.java46.carera.maintain.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.entity.Maintain;
import com.java46.carera.base.exception.BusinessException;
import com.java46.carera.base.mapper.MaintainMapper;
import com.java46.carera.base.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName MaintainService
 * @Description 维修管理Service
 * @Author c
 * @Date 2018/8/27 13:49
 * @Vcrsion 1.0
 **/
@Service
public class MaintainService {
    @Autowired
    private MaintainMapper maintainMapper;

    /**
     * 添加维修记录
     * @param maintain
     * @return
     */
    public JsonResult addMaintain(Maintain maintain){
        if (maintain == null){
            throw new BusinessException("添加的信息不能为空！");
        }
        maintainMapper.insert(maintain);
        return JsonUtil.setJsonResult(true,"添加成功！",null);
    }

    /**
     * 修改维修记录
     * @param maintain
     * @return
     */
    public JsonResult updateMaintain(Maintain maintain){
        if(maintain.getMaiid() == null){
            throw new BusinessException("缺少修改的必要参数！");
        }
        maintainMapper.updateByPrimaryKey(maintain);
        return JsonUtil.setJsonResult(true,"修改成功！",null);
    }

    /**
     * 删除维修记录
     * @param maiid
     * @return
     */
    public JsonResult deleteMaintain(Integer maiid){
        if(maiid == null){
            throw new BusinessException("缺少删除的必要参数！");
        }
        maintainMapper.deleteByPrimaryKey(maiid);
        return JsonUtil.setJsonResult(true,"删除成功！",null);
    }

    /**
     * 分页模糊查询
     * @param keyword
     * @param pageNum
     * @return
     */
    public JsonResult queryAll(String keyword, Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Maintain> maintains = maintainMapper.selectAll(keyword);
        PageInfo<Maintain> pages = new PageInfo<>(maintains);
        return JsonUtil.setJsonResult(true,"查询成功！",pages);
    }

    /**
     * 根据维修编号查询
     * @param maiid
     * @return
     */
    public JsonResult selectByPrimaryKey(Integer maiid){
        Maintain maintain = maintainMapper.selectByPrimaryKey(maiid);
        return JsonUtil.setJsonResult(true,"查询成功！",maintain);
    }
}
